<?php
namespace app\admin\model;

use think\facade\Db;
use think\facade\View;
use think\Model;
use think\model\concern\SoftDelete;

class Order extends Model
{
    use SoftDelete;
    protected $pk = 'order_id';
    protected $deleteTime = 'delete_time';
    public static function getOrderList($where,$data)
    {
        $list = Order::alias('o')
            ->field('o.order_id,o.order_sn,o.buyer_id,o.buyer_name,o.add_time,o.postscript,o.order_amount')
            ->field('os.order_status')
            ->join('order_status os','os.status_id=o.status')
            ->where($where)
            ->paginate(
                [
                    'list_rows' =>5,
                    'query'     =>$data,
                ]
            );

        return $list;
    }
    //订单状态表
    public static function getOrderStatus()
    {
        $list = Order::suffix("_status")
            ->field(true)
            ->select()
            ->toArray();
        return $list;
    }
    //订单详情查看
    public static function getOrderSelect($get)
    {
        $list = Order::alias('o')
            ->field('o.order_id,o.order_sn,o.buyer_id,o.buyer_name,o.add_time,o.postscript,o.order_amount,o.out_trade_sn')
            ->field('os.order_status')
            ->field('og.goods_name,og.goods_image,og.price,og.quantity')
            ->field('oe.region_name')
            ->join('order_extm oe','oe.order_id=o.order_id')
            ->join('order_status os','os.status_id=o.status')
            ->join('order_goods og','og.order_id=o.order_id')
            ->where('o.order_id','=',$get['id'])
            ->select()->toArray();
        $user = Order::alias('o')
            ->field('o.order_id,o.order_sn,o.buyer_id,o.buyer_name,o.add_time,o.postscript,o.order_amount,o.out_trade_sn')
            ->field('os.order_status')
            ->join('order_status os','os.status_id=o.status')
            ->where('o.order_id','=',$get['id'])
            ->find();
        return [$list,$user];
    }
    //获取器转添加时间
    public static function getaddTimeNextAttr($v,$data)
    {
        return date('Y-m-d H:i:m',$data['add_time']);
    }
    //处理发货
    public static function getOrderDeliver($get)
    {
        $edit = [
            'order_id'  =>$get['id'],
            'status'    =>'21'
        ];
        try{
            $res = Order::update($edit);
        }catch (\Exception $e){
            $res = false;
            echo $e->getMessage();
        }
        return $res;
    }
    //单个软删除订单
    public static function delOrder($get)
    {
        $res = Order::destroy($get['id']);
        return $res;
    }
    //多个软删除订单
    public static function delsOrder($get)
    {
        $id = explode(',',$get['id']);
        $res = Order::destroy($id);
        return $res;
    }



}